from django.contrib.auth import get_user_model
from rest_framework import serializers
from utils.now import my_now
from .models import *

User = get_user_model()


class SupplierSerializer(serializers.ModelSerializer):
    # queryset = Tag.objects.all()
    # tag = serializers.SlugRelatedField(slug_field='name',queryset=queryset,label='标签',many=True)

    def create(self, validated_data):
        id_pre = Supplier.objects.last().pk  # 新pk是表中最新一条数据的pk+1
        A001 = 'G-' + str(int(id_pre[2:]) + 1).zfill(5)
        validated_data['A001'] = A001
        return Supplier.objects.create(**validated_data)

    class Meta:
        model = Supplier
        fields = '__all__'


class ProductSerializer(serializers.ModelSerializer):
    queryset = Supplier.objects.all()
    F002 = serializers.SlugRelatedField(slug_field='B001',queryset=queryset,label='供应商')

    def create(self, validated_data):
        id_pre = ProductSource.objects.last().pk  # 新pk是表中最新一条数据的pk+1
        if id_pre[1:7] == my_now():
            A001 = 'S' + my_now()+str(int(id_pre[-3:]) + 1).zfill(3)
        else:
            A001 = 'S'+my_now()+'001'
        validated_data['A001'] = A001
        return ProductSource.objects.create(**validated_data)

    class Meta:
        model = ProductSource
        fields = '__all__'


class TagSerializer(serializers.ModelSerializer):
    queryset = Tag.objects.all()

    class Meta:
        model = Tag
        fields = '__all__'


class SupplierContactsSerializer(serializers.ModelSerializer):
    queryset = Supplier.objects.all()
    F001 = serializers.SlugRelatedField(slug_field='B001',queryset=queryset,label='供应商联系人')

    class Meta:
        model = SupplierContacts
        fields = '__all__'


class SupplierPictureSerializer(serializers.ModelSerializer):
    queryset = Supplier.objects.all()
    F001 = serializers.SlugRelatedField(slug_field='B001',queryset=queryset,label='供应商')

    class Meta:
        model = SupplierPicture
        fields = '__all__'
